package com.example.geometryshader;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ConfigurationInfo;
import android.opengl.GLSurfaceView;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import android.util.Log;
import android.view.MotionEvent;

public class MainActivity extends AppCompatActivity {

    private float mLastX = 0;
    private float mLastY = 0;
    private float mSensitivity = 0.2f;

    private GLSurfaceView mGLSurfaceView;
    private GLRender mGLRenderer;

    private final String LOG_TAG = "Activity";

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.i(LOG_TAG, "------- onCreate ----------");
        Log.d(LOG_TAG, "support opengl es 3.2 ? " + isOpenGLES32Supported(this));

        // 实例化GLSurfaceView (EGL处理opengl es 与窗口系统）
        mGLSurfaceView = new GLSurfaceView(this);
        mGLSurfaceView.setEGLContextClientVersion(3);

        // 设置渲染器
        mGLRenderer = new GLRender(this);
        mGLSurfaceView.setRenderer(mGLRenderer);

        setContentView(mGLSurfaceView);

        mGLSurfaceView.setOnTouchListener((v, event) -> {
            switch(event.getAction()){
                case MotionEvent.ACTION_DOWN:
                    // 当按下时的处理
                    this.mLastX = event.getRawX();
                    this.mLastY = event.getRawY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    // 当滑动时的处理
                {
//                    float deltaX = mSensitivity * (event.getRawX() - mLastX);
//                    float deltaY = mSensitivity * (event.getRawY() - mLastY);
//                    mGLRenderer.yrot += deltaX;
//                    mGLRenderer.xrot += deltaY;
//
//                    mLastX = event.getRawX();
//                    mLastY = event.getRawY();
                }
                break;
                case MotionEvent.ACTION_UP:
                    // 当抬起时的处理
                    break;
            }
            return true;
        });
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        mGLSurfaceView.onResume();
    }

    @Override
    protected void onPause()
    {
        super.onPause();
        mGLSurfaceView.onPause();
    }

    /**
     * @brief 检查是否支持opengl es 320
     * @param activity 当前活动
     * @return 是否支持
     */
    public static boolean isOpenGLES32Supported(Activity activity) {
        ActivityManager activityManager = (ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE);
        ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
        return (configurationInfo.reqGlEsVersion >= 0x00030002); // OpenGL ES 3.2 version is 0x00030002
    }
}